package com.vladimirkolarevic.eventplanner.spring_hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import com.vladimirkolarevic.eventplanner.Attend;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.AttendBean;
import com.vladimirkolarevic.eventplanner.service.AttendService;

public class AttendanceDAO implements AttendService {

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void attach(Attend a) {
		// TODO Auto-generated method stub

	}
	@Transactional
	public List<Attend> retrive() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createCriteria(AttendBean.class).list();
	}
	@Transactional
	public Attend retrive(Long id) {
		// TODO Auto-generated method stub
		return (Attend) sessionFactory.getCurrentSession().load(AttendBean.class, id);
	}
	@Transactional
	public Attend retrive(User user) {
		// TODO Auto-generated method stub
		Criteria crit = sessionFactory.getCurrentSession().createCriteria(AttendBean.class).createCriteria("user").add(Restrictions.like("id", user.getId()));
		return (Attend) crit.uniqueResult();
	}
	@Transactional
	public Long save(Attend attend) {
		// TODO Auto-generated method stub
		return (Long) sessionFactory.getCurrentSession().save(attend);
	}
	@Transactional
	public void update(Attend attend) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().update(attend);
	}

}
